<?php

namespace App\Admin\Renderable;

use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Table;
use Faker\Factory;
use Dcat\Admin\Widgets\Modal;
use App\Admin\Renderable\ReportDetail;
use App\Admin\Renderable\StudentDetailModal;
use Dcat\Admin\Admin;

class PostTable extends LazyRenderable
{
    public function render()
    {
    }

    public function makeSummerBoardFlow($obj, $detail_config)
    {
        $data = [];
        $title = ['期数','学员数','第一次出勤率','第二次出勤率','第三次出勤率','第四次出勤率','转化率'];
        $init = 0;
        foreach ($obj->channel as $k => $v) {
            $temp = [
                $v['name'],
                StudentDetailModal::makeModal($v['report'], $v['report_detail'] ?? [], $detail_config),
                StudentDetailModal::makeModal(getrate($v['sign1'] ?? 0, $v['report']), $v['sign1_detail'] ?? [], $detail_config),
                StudentDetailModal::makeModal(getrate($v['sign2'] ?? 0, $v['report']), $v['sign2_detail'] ?? [], $detail_config),
                StudentDetailModal::makeModal(getrate($v['sign3'] ?? 0, $v['report']), $v['sign3_detail'] ?? [], $detail_config),
                StudentDetailModal::makeModal(getrate($v['sign4'] ?? 0, $v['report']), $v['sign4_detail'] ?? [], $detail_config),
                StudentDetailModal::makeModal(getrate($v['convert'] ?? 0, $v['report']), $v['convert_detail'] ?? [], $detail_config),

                StudentDetailModal::makeModal($v['new'] ?? 0, $v['new_detail'] ?? [], $detail_config),
                StudentDetailModal::makeModal($v['old_summer'] ?? 0, $v['old_summer_detail'] ?? [], $detail_config),
                StudentDetailModal::makeModal($v['od_flow'] ?? 0, $v['od_flow_detail'] ?? [], $detail_config),
                StudentDetailModal::makeModal($v['long'] ?? 0, $v['long_detail'] ?? [], $detail_config),
                StudentDetailModal::makeModal($v['study'] ?? 0, $v['study_detail'] ?? [], $detail_config),
            ];
            $data[] = $temp;
            $init = 1;
        }
        $table = new Table();
        $table = $table->make($title, $data);
        return $table;
    }

    public function makeChannelBoardFlow($obj, $detail_config, $list_config)
    {

        $data = [];
        $title = array_merge(['二级维度'], array_column($list_config, 'title'));
        $init = 0;
        foreach ($obj->channel as $channel_id => $date_arr) {
            $temp = [];
            array_push($temp, $date_arr['name']);
            foreach ($list_config as $v) {
                if ($v['type'] == 'detail') {
                    array_push($temp, StudentDetailModal::makeModal($date_arr[$v['key']], $date_arr[$v['key'].'_detail'], $detail_config),);
                } else {
                    array_push($temp, $date_arr[$v['key']]);
                }
            }
            foreach ($date_arr as $datev => $v) {
                if (!isset($v['num'])) {
                    continue;
                }
                $num = $v['num'];
                if ($num == 0) {
                    $num = '';
                } else {
                    $num = Modal::make()
                    ->lg()
                    ->title('流量详情')
                    ->body(ReportDetail::makeCustomerDetail($v['detail'], $detail_config))
                    ->button('<span">'.$num.'</span>');
                }
                
                array_push($temp, $num);
                if ($init == 0) {
                    $datev = date('d', strtotime($datev));
                    array_push($title, $datev);
                }
            }
            $data[] = $temp;
            $init = 1;
        }
        $table = new Table();
        $table = $table->make($title, $data);
        return $table;
    }

    #客资汇总
    public function makeChannelBoardSource($obj)
    {
        // dd($obj);
        $data = [];
        $title = ['渠道','客资','约课','签到','转化','约课率','出勤率','客资转化率'];
        $init = 0;
        foreach ($obj->channel as $channel_id => $date_arr) {
            $temp = [
                $channel_id,
                $date_arr['source'],
                $date_arr['report'],
                $date_arr['sign'],
                $date_arr['convert'],
                $date_arr['report_rate'],
                $date_arr['sign_report_rate'],
                $date_arr['convert_source_rate'],
            ];
            foreach ($date_arr as $datev => $v) {
                if (!isset($v['num'])) {
                    continue;
                }
                $num = $v['num'];
                if ($num == 0) {
                    $num = '';
                } else {
                    $num = Modal::make()
                    ->lg()
                    ->title('流量详情')
                    ->body(ReportDetail::makeChannelDetailSource($v['detail']))
                    ->button('<span style="color:#2196f3;">'.$num.'</span>');
                }
                array_push($temp, $num);
                if ($init == 0) {
                    $datev = date('d', strtotime($datev));
                    array_push($title, $datev);
                }
            }
            $data[] = $temp;
            $init = 1;
        }
        $table = new Table();
        $table = $table->make($title, $data);

        // dd($title);
        return $table;
    }


}
